package com.zcpt.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zcpt.dao.SponsorDao;
import com.zcpt.exception.AlterException;
import com.zcpt.exception.InsertException;
import com.zcpt.exception.SystemException;
import com.zcpt.pojo.Sponsor;
import com.zcpt.service.SponsorService;

@Service("sponsorService")
public class SponsorServiceImpl implements SponsorService {
	@Resource // 自动注入dao
	private SponsorDao sponsorDao;

	@Override // 根据id查找发起者
	public Sponsor getSponsorById(int sponsorId) {
		return sponsorDao.querySponsorById(sponsorId);
	}

	@Override // 根据id查找发起者和对应项目信息
	public Sponsor getSponsorByIdWithProject(int sponsorId) {
		return sponsorDao.querySponsorByIdWithProject(sponsorId);
	}

	@Override // 查找所有发起者信息
	public List<Sponsor> getAllSponsor() {
		return sponsorDao.queryAllSponsor();
	}

	@Override // 是否成功添加发起者信息
	public boolean addSponsor(Sponsor sponsor) {
		try {
			// 添加信息
			int insertCount = sponsorDao.insertSponsor(sponsor);
			// 判断
			if (insertCount <= 0) {
				throw new InsertException("添加数据失败！");
			} else {
				return true;
			}
		} catch (InsertException e) {
			throw e;
		} catch (Exception e) {
			throw new SystemException("系统故障:" + e.getMessage());
		}
	}

	@Override // 是否成功修改发起者信息
	public boolean alterSponsor(Sponsor sponsor) {
		try {
			// 修改信息
			int updateCount = sponsorDao.updateSponsor(sponsor);
			// 判断
			if (updateCount <= 0) {
				throw new AlterException("修改数据失败！");
			} else {
				return true;
			}
		} catch (AlterException e) {
			throw e;
		} catch (Exception e) {
			throw new SystemException("系统故障:" + e.getMessage());
		}
	}

}
